Wordpress Custom fields display file URL - php

I have the following code and need to show THREE things from my custom post type called fact-sheet.
The Title
The summary (fact_sheet_summary)
A file upload URL (fact_sheet_pdf_link)
I can get the first two to work but no idea how to do the third.
Basically my output should be...
The Title
The Summary paragraph
Click here to download as PDF
Any ideas how I can query to list all of these post type results? Is there a better way to do this rather than what I have below? The main problem is, I can't get the URL of the uploaded file.
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'fact-sheet',
'order' => 'ASC',
));
if($posts)
{
foreach($posts as $post)
{
echo '<span class="fact-sheet-title">' . get_the_title($post->ID) . '</span><br />';
echo '<p><span class="fact-sheet-summary">' . the_field('fact_sheet_summary') . '</span></p>';
}
}
?>

I think It's better to use query_posts() as you can use The Loop default structure.
As for the custom fields (Using ACF Plugin), you're using the_field(), which automatically echoes the retrieved field value. You can use, in other hand, the get_field() function, which just returns the value of the field.
You could do something like this:
// Query all posts from 'fact-sheet' post_type
query_posts(array(
'numberposts' => -1,
'post_type' => 'fact-sheet',
'order' => 'ASC',
// Getting all posts, limitless
'posts_per_page' => -1,
));
// Loop throught them
while(have_posts()){
the_post();
echo '<span class="fact-sheet-title">' . get_the_title() . '</span><br />';
echo '<p><span class="fact-sheet-summary">' . get_field('fact_sheet_summary') . '</span></p>';
// Echos the link to PDF Download
echo '<p>Click here to download as PDF</p>';
}
// Once you're done, you reset the Default WP Query
wp_reset_query();
In case you might need further explanation about wp_reset_query(), check this out.

Can you try this? It's slightly modified from the manual of the WP Codex (not much)
<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$args = array(
'post_type' => 'attachment',
'post_mime_type' => array('application/pdf'),
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<li>';
the_attachment_link( $attachment->ID, true );
echo '<p>';
echo apply_filters( 'the_title', $attachment->post_title );
echo '</p></li>';
}
}
endwhile; endif; ?>
</ul>
If it works, maybe it's better to modify a working sample to your needs - by adding your custom fields. Just my thoughts :-)

Related

Wordpress how to display hierarchy of pages with a WP Query-Parent->Child Relationship

I'm currently trying to fetch all my pages from different post types and display them on a single page with a WP Query but I wanted to display them in such a template in the HTML instead of it all coming as a big list of course I'm not entirely sure if this is even possible
Parent
Child
Parent
Child
Parent
Parent
Parent
This is what I'm using so far
$args = array(
'post_type'=> 'page',
'orderby' => 'title',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => 20,
);
$result = new WP_Query($args);
?>
<section>
<h2><?php echo $post_type; ?></h2>
<?php
while ($result->have_posts()) {
$result->the_post();
$post = get_post();
?>
<ul>
<li class="">
<?php echo the_title(); ?>
</li>
</ul>
<?php
}
wp_reset_postdata();
?>
</section>
<?php }
I tried using wp_list_pages but I'm not sure how to use that for different post types and also I would like to have limited posts per page not display everything
WP_Query is not the right tool for the job!
Wordpress has special functions for what you're looking for:
get_pagesDocs
wp_list_pagesDocs
"I tried using wp_list_pages but I'm not sure how to use that for different post types"
Well, that's why docs are here for!
For example, wp_list_pages accept an argument called post_type which lets you specify which "post type" you're interested in, and its default value is the wordpress "page" type. If you want to change it to your custom post type then you could use something like this:
$args = array(
'post_type' => 'your_custom_post_type'
);
wp_list_pages($args);
"also I would like to have limited posts per page not display everything"
Again, there is docs for that!
if you use get_pages for example, then there is an argument for limiting the number of pages shown on the page and that's called number. The number argument is an integer and represents the number of pages to return and its default is 0(all pages).
Using get_pages also you could specify your custom post type to query. The argument for that is called post_type and its default value is the wordpress "page" type.
$args = array(
'post_type' => 'your_custom_post_type',
'number' => 20
);
$your_pages = get_pages($args);
foreach ($your_pages as $page) {
echo $page->post_title;
}
If you really need/want to use WP_Qery then you could do it like this:
WP_Qery + get_pages
$args = array(
'post_type' => 'page',
'orderby' => 'title',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => 20,
);
$results = new WP_Query($args);
if ($results) {
while ($results->have_posts()) {
$results->the_post();
echo "<h3>" . get_the_title() . "<h3>";
$sub_pages_args = array(
'child_of' => get_the_ID(),
);
$sub_pages = get_pages($sub_pages_args);
echo "<ul>";
foreach ($sub_pages as $sub_page) {
echo "<li>";
echo $sub_page->post_title;
echo "</li>";
}
echo "</ul>";
}
}
wp_reset_postdata();
Which will output this:
Parent Title
Child Title
Parent Title
Child Title
Parent Title
Parent Title
Parent Title
Fully tested and works on wordpress 5.8.
$pages = get_pages();
if ($pages) {
foreach ($pages as $page) {
if ($page->post_parent == 0) {
echo $page->post_title . ' — ' . $page->ID;
$sub_pages = get_pages(array('child_of' => $page->ID));
foreach ($sub_pages as $sub_page) {
echo ' — ' . $sub_page->post_title . ' — ' . $sub_page->ID;
}
}
}
}

My php-wordpress if while loop only runs the first time

My code had a typo!
But im not allowed to delete this so here you have some info on ways you could have done a better job then me ;)
The problem is as follows, My query is skipping a part of my loop.
i have a query that makes a anchor with the title and thumbnail of the post.
it runs the query fine for the first post except for the second post it does not load in the thumbnail, but it does show the title, and the title is only mentioned after the thumbnail, There is no small image that resembles it cant be found either. My first question posted on here so apolagies for misplacing items in the wrong sections.
<?php
// WP_Query arguments
$args = array (
'post_type' => array( 'klantcase' ),
'post_status' => array( 'publish' ),
'nopaging' => true,
'order' => 'ASC',
'orderby' => 'menu_order',
);
// The Query
$klantcases = new WP_Query( $args );
// The Loop
if ( $klantcases->have_posts() ) {
while ( $klantcases->have_posts() ) {
$klantcases->the_post();
echo "<a href=".get_the_permalink().">";
echo get_the_post_thumbnail( null, $size = 'post-thumbnail');?><br><?php
echo the_title();?><br><?php
echo "</a>";
}
} else {
echo "no posts found";
}
// Restore original Post Data
wp_reset_postdata();
Now there is probably many ways to improve this loop but as mentioned im very new to all of this. That said i'd love to hear how you guys would resolve this issue.
Define empty variable on top of while loop.
And in loop concatenate your html with that empty variable.
For Example.
$output = '';
if ( $klantcases->have_posts() ) {
while ( $klantcases->have_posts() ) {
$klantcases->the_post();
$output .= '<a href="'.the_permalink().'">';
$output .= the_post_thumbnail() .'<br>';
$output .= the_title();
$output .= '</a>';
}
} else {
echo "no posts found";
}
echo $output;
You don't need to echo the_title() - it already echoes out, so I'm guessing that's where part of the problem is coming in. You can also make your loop a little more simple.
You don't need the 'post_status' argument, as publish is default.
In your loop, let's use WP's built in echo functions: the_permalink(), the_title() and the_post_thumbnail(). You don't have to pass any arguments to the the_post_thumbnail() because you are just calling the default in your code.
<?php
// WP_Query arguments
$args = [
'post_type' => ['klantcase'],
'nopaging' => TRUE,
'order' => 'ASC',
'orderby' => 'menu_order',
];
// The Query
$klantcases = new WP_Query( $args );
// The Loop
if ( $klantcases->have_posts() ) {
while ( $klantcases->have_posts() ) {
$klantcases->the_post(); ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?><br>
<?php the_title(); ?>
</a>
<?php
}
} else {
echo 'no posts found';
}
// Restore original Post Data
wp_reset_postdata();

wordpress shortcode render content in dashboard also

I am using a simple wordpress shortcode
function my_recent_post()
{
echo 'hello';
}
add_shortcode( 'recent', 'my_recent_post' );
with the shortcode [recent] and its working fine and visible in front page,
but the problem is, its printing the hello in the dashboard also.
below is the screenshot, can anyone please help.
Update:
I was actually trying to show posts, so can you help me with this, because it renders the lists of posts in the dashboard itself like the "hello". I tried:
function lorem_function() {
global $post;
$args = array( 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' );
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post ); ?>
<div>
<?php the_date(); ?> <br /> <?php the_title(); ?> <?php the_excerpt(); ?>
</div>
<?php endforeach;
wp_reset_postdata();
return;
}
add_shortcode('lorem', 'lorem_function');
Based on your comments to me & Nikita Dudarev, what you need to do is create a variable to hold all the post information and then return it. Using the function you posted as an example:
function lorem_function() {
global $post;
$args = array( 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' );
$postslist = get_posts( $args );
// create a variable to hold the post information
$html ="";
foreach ( $postslist as $post ) :
setup_postdata( $post );
$backgroundstyle = "";
// get the featured image and set it as the background
if ( has_post_thumbnail() ) { // make sure the post has a featured image
$imageurl = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium' ); // you can change "medium" to "thumbnail or full depending on the size you need
// add the css for the background image. You can include background-size etc ad required
$backgroundstyle = "background-image: url('".$imageurl[0]."');";
}
// add the information to the variable
$html .= '<div style="'.$backgroundstyle.'">';
$html .= get_the_date();
$html .= "<br />";
$html .= get_the_title();
$html .= get_the_excerpt();
$html .= "</div>";
endforeach;
wp_reset_postdata();
return $html;
}
add_shortcode('lorem', 'lorem_function');
Note that the_date(), the_title() and the_excerpt() all display the information (just like echo).
Instead you must use get_the_date(), get_the_title() and get_the_excerpt() - these get the same information, but instead of displaying it directly, they return it as a variable which you can then store in your html string to be returned.
Update:
As you don't want to use the variable name on each line for whatever reason, you can do it like this:
$html .= "<div>".get_the_date()."<br />".get_the_title().get_the_excerpt()."</div>";
I'm not sure why you specifically want to change it to do that - it makes absolutely no difference to how it works, it just makes it harder to read and identify any errors :-)
Your function must return a value, not output
function my_recent_post()
{
return 'hello';
}
add_shortcode( 'recent', 'my_recent_post' );

Custom wordpress function to display related posts based on multiple tags

I'm building a wordpress site and want to show related posts based on tags when viewing a single post. Currently I'm using the below function to do this and it works.
My issue is that I want to prioritise related posts that have the most tags in common. Usually I have 3-5 tags for each posts so I want to display posts with 3 tags in common before 1 tag for example. I've tried some different code snippets that should be able to do this but I haven't managed to get it to work so far.
Any help will be appreciated, thanks.
function smak_related_posts() {
global $post;
$tags = wp_get_post_tags( $post->ID );
if($tags) {
foreach( $tags as $tag ) {
$tag_arr .= $tag->slug . ',';
}
$args = array(
'tag' => $tag_arr,
'numberposts' => 4,
'post__not_in' => array($post->ID),
'post_type' => array('post', 'projects'),
);
$related_posts = get_posts( $args );
if($related_posts) {
echo '<footer class="entry-footer">';
echo '<h3>Se også...</h3>';
echo '<div id="masonry-loop">';
get_template_part( 'template-parts/content', 'masonry_sizer' );
foreach ( $related_posts as $post ) : setup_postdata( $post );
get_template_part( 'template-parts/content', 'masonry' );
endforeach; }
}
echo '</div>';
echo '</div>';
wp_reset_postdata();
}

Wordpress loop media by "uploaded By"

I have a webpage that I want to display images that are uploaded by a certain author.
In the backend if I look at media, each image has an 'Uploaded By' attribute, and then it says the authors name.
(source: discoveryourwonder.com)
I've tried using this loop:
<?php
// The Query
$args = array(
'author' => $author_name_variable, // Replace with author id
'post_status' => 'any',
'post_type' => 'attachment'
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . the_content() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
It is very buggy. Some authors it will show every media file, others none; the rest are just inaccurate. It's a real blind shot :/
The goal is to loop through all the media files, and then post the_content() of all files with the corresponding Uploaded By name.
I would be grateful if someone could comment as to why an ID is more reliable than a slug in the 'author' argument.
I figured out a solution. Apparently the 'author' argument does not like usernames, so I converted it to ID and it works now. I used get_user_by( 'slug', $username ); to get all the information of the particular username, and then assigned that array to a variable. I then filtered the variable to only use the ID and passed that through the arguments.
Here's the working loop:
<?php
// The Query
$profileid = get_user_by( 'slug', $username );
$args = array(
'author' => $profileid->id, // Replace with author id
'post_status' => 'inheret',
'post_type' => 'attachment'
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . the_content() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>

Categories