Home  >  Article  >  Software Tutorial  >  How to call all attached images in a single post in WordPress

How to call all attached images in a single post in WordPress

PHPz
PHPzforward
2024-01-14 15:57:33568browse

How to call all attached images in a single post in WordPress

How does wordpress call all attached images in a single article

All attachments of an article are identified through the post_parent hook in the POSTS data table, and the post_type is attachment.

After understanding this sentence, check the following code:

/* Get all image attachments under the specified post. */

$attachments = get_children(

array(

'post_parent' => Specify the ID of the log,

'post_status' => 'inherit',

'post_type' => 'attachment',

'post_mime_type' => 'image',

'order' => 'ASC',

'orderby' => 'menu_order ID',

'suppress_filters' => true

)

); The next step is foreach to read out the relevant information of the attachment.

How to replace or delete WordPress in bulk

We usually manually insert some of the same text or shortcode into the WordPress article content, because this method is not fixed and is very flexible. For example, different articles often have the same text (some words or phrases), and sometimes I want to replace them with other texts, but there are many articles and it is impossible to modify them one by one; for example, I used to use shortcodes to embed Google The map is very convenient. But later I no longer wanted to use this method, so I removed the corresponding functions that had been placed in functions.php. Unexpectedly, the shortcodes called before were still left in many articles, allowing visitors to see a lot of It’s unsightly. What should I do if it’s time-consuming and laborious to delete the files one by one manually?

This article explains two simple methods. Using only a few simple lines of code, you can easily replace the same text and shortcodes in many articles in batches.

1. Batch replace text content in articles

Add this function to the theme’s functions.php file:

function replace_text_wps($text){

$replace = array(

// 'I am the text to be replaced' =>'I am the text to be replaced'

'wordpress' =>'',

'excerpt' =>'',

'function' =>''

);

$text = str_replace(array_keys($replace), $replace, $text);

return $text;

}

add_filter('the_content', 'replace_text_wps');

add_filter('the_excerpt', 'replace_text_wps');

Note: wordpress, excerpt and function in the fourth, fifth and sixth lines of the above code are the texts to be replaced; correspondingly, and are the replaced texts. It is clear that they have been replaced with link text. . The meanings of the last two functions are:

1.add_filter('the_content', 'replace_text_wps'); ——>Replace the text content in the article body

2.add_filter('the_excerpt', 'replace_text_wps'); ——>Replace the text content in the article summary

Via wprecipes

2. Delete or replace the text content in articles in batches (here, we take deleting shortcodes as an example):

There may be many methods, but I think this is the best, which is to execute SQL query statements for batch deletion. It is very thorough, but it involves important data tables of the WordPress database, so you must make a backup of the database in advance.

Log in to the database management center and execute the following statement:

UPDATE wp_posts

SET post_content = REPLACE( post_content, '[adsense]', '' );

Note: What I want to delete here is a shortcode "[adsense]" that is no longer useful. Please modify it to the content you want to delete when using it.

If we want to replace [adsense] with [related_posts], we can write it like this:

UPDATE wp_posts

SET post_content = REPLACE( post_content, '[adsense]', '[related_posts]' );

How does wordpress call articles based on author

wordpress calls the article link, title and content of the specified ID. I found that the method yesterday was not good enough. Today I found a better one. I will try another one later: wordpress calls the page link, title and content of the specified ID.

$post_id = 1; // Post ID, can be found in the WP background

echo get_post( $post_id )->post_content; // Output the content of the article

?>

Here is all the information about the article:

// Calling method:

echo get_post( $post_id )->post_title; // Output the title of the article

// post_author: The number of the author of the article

// post_data: The date and time the article was published (YYYY-MM-DD HH-MM-SS)

// post_data_gmt: Greenwich Mean Time (GMT) when the article was published (YYYY-MM-DD HH-MM-SS)

// post_content: article content

// post_title: article title

// post_category: The number of the article category. Note: This value is always 0 in versions after WordPress 2.1. You can use the get_the_category() function when defining the category of an article.

// post_excerpt: article summary

// post_status: article status (publish|pending|draft|private|static|object|attachment|inherit|future)

// comment_status: comment status (open|closed|registered_only)

// ping_status: pingback/trackback status (open|closed)

// post_password: Post password

// post_name: URL nesting of the article

// to_ping: URL link to be quoted

// pinged: quoted links

// post_modified: The last modified time of the article (YYYY-MM-DD HH-MM-SS)

// post_modified_gmt: The last modified GMT time of the article (YYYY-MM-DD HH-MM-SS)

// post_parent: parent article number (for attachments, etc.)

// guid: A link to the article. Note: You cannot use a GUID as a permanent link (although it was indeed treated as a permanent link in versions prior to 2.5), nor as an available link to an article. A GUID is a unique identifier that currently happens to be a link to an article.

// post_type: (char) (log | page | attachment)

How to use WPAutoPost to collect web pages and automatically update them to wordpress

1. Install WP-AutoPost

You can install WP-AutoPost manually by downloading the ZIP file from your WP-AutoPost.ORG account.

If you want to install WP-AutoPost manually, please follow these steps:

Log in to your WP-AutoPost.ORG account.

Go to the "Account" section.

Download the WP-AutoPost plugin you need (ZIP file).

Upload the downloaded ZIP file to your website's 'wp-contents/plugins' directory.

Extract the ZIP file. This will create a directory.

Log in to your website, go to "Plugins", and enable it.

2. Create tasks and basic settings

Create Task

After clicking "New Task", enter the task name to create a new task. After creating the new task, you can view the task in the task list and make more settings for the task.

basic settings

After clicking "Settings", you can make the following settings under the "Basic Settings Tab":

Task name: Modify task name

Category directory: The category directory where the articles collected by this task are published

Author: The author of the article collected by this task must be a registered user in wordpress

Update interval: How long to check whether there are new articles under this collection task that can be updated

Character set: Collect the character set encoding of the target website. The default is UTF8. If the character set encoding of the target web page is not UTF8, the captured web page will be garbled. This problem can be solved by setting the correct character set (how to set the characters correctly set)

Download remote pictures: If the articles collected under this task contain pictures, you can choose whether to download remote pictures to the local server. If you choose to download remote pictures, you can make the following more settings:

Save the downloaded images to the wordpress media library

Save image to Flickr

Save the image to Qiniu Cloud Storage

Automatically set the first downloaded image as the featured image

Downloaded pictures are automatically added with watermarks, text watermarks or image watermarks can be added

Choose to set the attribute of the image address (if the attribute containing the source image address is not src, you can set it here)

Download remote attachments: If the articles collected under this task contain other types of attachments, you can choose whether to download these attachment files to the local server. After selecting the download, you can make the following more settings:

Save the downloaded attachment information to the wordpress media library

Auto tag: Choose whether to use automatic tags

Tag list: After using automatic tags, if the article contains the keywords in the list, tags

will be added automatically.

Match complete words: This setting is valid for English articles, please do not enable this setting for Chinese articles

Automatic summary: The first paragraph or the first few paragraphs of text in the article can be automatically set as the summary

Publication status: Collect the status of the article after publication, which can be: published, draft, waiting for review

Manual selective collection: Select Yes to manually select which articles can be collected and published

3. Article source settings

Article Source Settings

Under this tab we need to set the article list URL of the article source and the matching rules for specific articles

The above is the detailed content of How to call all attached images in a single post in WordPress. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:docexcel.net. If there is any infringement, please contact admin@php.cn delete