This is my first post on stackoverflow, as I am quite new to PHP. I am learning the language to help me customize my online portfolio in Wordpress, and I usually manage to make the changes I need - but not this time, apparently.
I am trying to use get_post_meta to read a meta tag in my portfolio pages, and avoid displaying the page thumbnail. This is the code I am using:
<?php $disable_thumb = get_post_meta( get_the_ID(), 'minimal_portfolio_page_thumb', true );
if( $disable_thumb !== 'on' ): ?>
<?php if ( has_post_thumbnail() ) : ?>
<div class="post-thumbnail">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php endif; ?>
<?php endif; ?>
Using a meta tag management plugin, I added the following tag to all my "portfolio"-type pages:
<meta name="minimal_portfolio_page_thumb" content="on">
I am currently checking if this works in this page of my web: egozalor.com/portfolio/hansel-gretel/
Long story short, the trick does not work as expected. I guess there is something I am doing wrong or not realizing, due to my little knowledge of PHP. Any indications, tips or recommendations are very welcome!
Also please let me know if further or more specific information is necessary to assess my issue.
Thanks in advance!
The function get_post_meta has nothing to do with the <meta> elements of your site. With this function you can only get metadata of the post itself. You can set the metadata on each post at the end (custom fields) as key / value pairs.
These custom fields are not visible on the site itself. You can create a custom field on every post with key minimal_portfolio_page_thumb and value on (or another value like 0/1).
Looks like the custom fields are disabled by default on WordPress. But you can enable the custom fields without an additional plugin. On the top right of your post you can find three dots to open a menu. At the end of the menu there is the entry "Options". On the options you can enable the custom fields.
You can enable the custom fields for posts and pages.
Related
I've made a custom post type called 'news', plus an index page called 'archive-news'. In the archive page the excerpt is replaced with the content of the post. Can't work out how to change it.
I've been using HTML for over a year, familiar with the idea of Wordpress PHP but probably not deep enough yet. Had a look for similar problems. The input area for the excerpt has not been left blank (when you edit a custom post). I've heard that if it doesn't then it defaults to use content instead of excerpt. Cleared the cache many times and given it time to refresh.
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="postExcerpt">
<p><?php the_excerpt(); ?></p>
</div>
<?php endwhile; wp_reset_query(); ?>
should show the excerpt and not the content in the archive-news
I am trying to deal with two get_post functions, in single.php the first get_post function is the post from the wordpress, But after that I called get_post function to other post also to use both of them in the same page but after I call the first get_post ( the main post ) I get the only second data and cant reach the first data.
My code called to the second function ( The first is from wordpress post):
$main_post = get_field('main_post');
$main_p = get_post($main_post->ID);
Then I am trying to use the variable $post OR the_title() OR any other functions to get the first post and it always returning the info of the $main_p post
for example
get_the_title( get_post()->ID )
returns the $main_p post id and not the main post of the single.php
any soulutions ?
I may be wrong, but it seems to me that you are trying to post a different post format with normal post format?
I, myself use get_post_format() so it can be styled differently or have different options.
in single.php I use
<!-- checking if there are any blogposts to be shown using have_posts check which is a wordpress function-->
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?> <!-- the correct syntax for while loop in wordpress to show all the blogposts -->
<?php get_template_part('content', get_post_format()); ?>
<?php endwhile; ?>
<?php else : //else stament if there aren't any posts (put inside if before endif)?>
<p><?php __('No Posts Found'); ?></p>
<?php endif; ?> <!-- stop checking for blog posts-->
</div><!-- /.blog-main -->
Inside functions.php I activated the post-formats inside wp_theme_setup() function
add_theme_support('post-formats', array('aside', 'gallery'));
In this case i activated the gallery and aside posts
That way I have 2 different post types on one page
like this
image from my theme blog page
Here is also a video tutorial on post formats from Traversy media
https://www.youtube.com/watch?v=CRa7eiqyiCM&list=PLc5p9nvpdwBlrNU0hr1f0kXPRkh0aGo1Q&index=7
The key reason why your post values are being overwritten, the additional get_post() declarations are overriding the default query. Now, the code in your pastebin is a pretty massive dog's breakfast, so a direct solution is a rather large undertaking (e.g. the indentation is all over the place, the code snippets are less than ideal regarding their readability, etc...). However, I can point you in the right direction for the solution.
When I pull content from another page on my WordPress sites, I avoid using get_post() in favour of declaring a fresh new WP_Query() (that's just my preference), following it up with a wp_reset_postdata() declaration.
Here's an example of multiple queries on a single template in the WordPress codex:
https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops
The key here is the wp_reset_postdata(). I'd recommend looking into it's purpose. It'll save you a lot of grief:
https://codex.wordpress.org/Function_Reference/wp_reset_postdata
I have created a custom post type named as product. After that I have created a template file to show all products on that page and write the below code :
<?php $loop = new WP_Query( array( 'post_type' => 'acme_product',
'posts_per_page' => 14 ) );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div>
<div>
<?php the_post_thumbnail('thumbnail');
?>
</div>
<div>
<?php the_title( '<h2 class="entry-title"><a href="'.get_permalink().'" title="'.
the_title_attribute( 'echo=0' ).
'" rel="bookmark">', '</a></h2>' );
?>
</div>
</div>
<?php endwhile;
?>
But when I click on the product title link it will not show my product details page. Can anyone help me?
You need to save your template as single-{post_type}.php so in your case it would be single-acme_product.php and make sure that permalink is enable if not than
Login to your WordPress admin.
Go to Settings -> Permalinks. And under Common Settings, let’s use Post name.
Then click ‘Save Changes’
as per your code you don't need to define the post_type here. just use with simple code
<?php while ( have_posts() ) : the_post(); ?>
It sounds as though you've already figured out how to loop through your product type. If you're new to custom post types and are not already aware of it, I would suggest investigating the "archive" template for custom post types (archive-{post_type}.php). I saw another answer that referenced single-{post_type}.php as well; both of these can be identified in the Template Hierarchy documentation in the WP Codex. By default, if you don't provide a customized single template for your post type, it will fall back to single.php (which may require customization if you want it to perform double-duty for multiple post types).
With that in mind, and assuming you're already using single-product.php in your theme (or have otherwise coaxed Wordpress into using your template), it would be helpful to know the symptoms you're experiencing:
Are you getting a 404? This could be a permalink issue, and if you haven't already attempted it you might consider flushing your rewrite rules. This can be accomplished by simply visiting the Settings -> Permalinks panel in the Dashboard.
Is your template being used? Consider adding some HTML comments to your template, and view source on the rendered version to ensure that you're even using the correct file. Failure to load the appropriate template could be due to a filename typo or some other logic that interferes with the normal template selection logic.
If you're getting the correct template, but you're not able to display the correct details, are you doing anything particularly wonky with your single template's loop?
Without clear details on what symptoms you're actually experiencing, it's hard to give you better direction. Hopefully some of this information points you to the correct answer, but if not please share additional details as to what is happening on your end.
This is my first question on Stack Overflow so bear with me.
I have a WordPress site with the Advanced Custom Fields plugin installed. The site has a custom post type that has various ACF custom fields attached to it.
Not all of the custom fields are required, also some of the custom fields are grouped into their own tabular structure.
I need to check if either of 2 custom fields have content, and if they do display the table.
My PHP is fairly limited but I did some research and it should work, but its not.
The code is as follows:
<?php if (get_field( 'meeting_documents_agendas' && 'meeting_documents_minutes')) : ?>
<?php if ( get_field( 'meeting_documents_agendas' ) ) : ?>
Download file
<? endif ?>
<?php if ( get_field( 'meeting_documents_minutes' ) ) : ?>
Download file
<? endif ?>
<? endif ?>
Basically nothing is displaying, even if I have content in those custom fields.
Is the code right? Could it be a WordPress bug?
Try
<? if (get_field('meeting_documents_agendas') || get_field('meeting_documents_minutes')): ?>
This means: if one or both of the fields are set, display the table.
Don't use && because this means that both fields must be set. Use get_field() twice because you want to logically link the results of get_fields(), not the parameters themselves.
This was helpful for me as well. Though my issue was a bit different since I was using sub_fields. I needed the code to check if both sub_fields had data.
Here's my code.
<?php if (get_sub_field( 'additional_link_url' ) && get_sub_field('additional_link_text') ) : ?>
<br /><a target="_blank" href="<?php echo get_sub_field('additional_link_url')['url']; ?>"><?php echo get_sub_field('additional_link_text'); ?></a>
<?php endif; ?>
If you look at my site, I have an area named Top Winter Projects in the right-side navigation. I would like to automate this part of the site as I currently have to manually paste HTML into that particular section. What I want to be able to to do is have those images (which should link to the corresponding post) change based on date.
I've tried using the featured image plugin, but that doesn't seem to work with my theme for some reason. Is there an alternate route?
I've tried piecing some code together, but i'm a bit clueless when it comes to php. I'm also not sure how i'd get a picture to show up. Here is what I have, where 40, 50, & 60 are three posts. Can echo calls show pictures?
<? php
$args = array('include' => 40, 50, 60 )
echo get_posts($args);
?>
Here is my website: http://www.merrimentdesign.com
Thanks, any help is appreciated.
One way to do it is to create a loop using WP_Query and then you can put whatever content you want in the loop. I typically do this and use categories to control what posts are shown in each area. So for example if you created a category called featured, you could tag every post you want to show in that section with that category. Then you could create a simple loop like :
<?php $the_query = new WP_Query('category_name=featured&showposts=10'); ?>
<?php while ($the_query->have_posts()) : $the_query->the_post();?>
<a href="<?php the_permalink() ?>" rel="bookmark" class="postTitleLink">
<?php the_post_thumbnail(); ?>
</a>
<?php endwhile; ?>
This is assuming you have the thumbnail function enabled in your themes functions.php.
add_theme_support( 'post-thumbnails' );