I am writing a wordpress plugin and I want to hide posts/pages which contain a specific string in their title.
For example, I want to hide all the posts/pages where the title contains: [something123]
show this post/page "Lorem Ipsum"
hide this post/page "Lorem Ipsum [something123]"
hide this post/page "Hello World [something123]"
show this post/page "Hello World"
In your theme directory find loop.php
In it there should be a line like this
<?php while ( have_posts() ) : the_post(); ?>
pseudocode:
if (get_the_title() contains "") { endwhile; }
http://codex.wordpress.org/Function_Reference/get_the_title
You could also use a filter:
http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
Although I don't feel like writing one for you =). Maybe somebody else will.
Related
In the theme, I use get_the_excerpt in index.php, where I use the loop to display the titles, excerpts, ... of several post and it works as expected. The excerpt/teaser is manually marked with the "read more" tag.
However, I also would like to use get_the_excerpt in single.php but instead of receiving only the text up to the "read more" tag, I get the first 55 words.
The documentation says:
The <!--more--> quicktag will not operate and is ignored in Templates where just one post is displayed, such as single.php.
Ok, so what can I do if I want the excerpt to contain the text only up to the quicktag in single.php?
I believe you can accomplish what you want with the built-in functionality of WordPress, utilizing the $more variable.
In order to show the content before the more quicktag, you should be able to do this on your single.php:
// Declare global $more (before the loop).
global $more;
while( have_posts() ) {
// Set (inside the loop) to display content above the more tag.
$more = 0;
// call the_post() to prepare post variables / functions
the_post();
// output your markup, post title, etc...
// then this the_content() should render only the content before the more quicktag
the_content( 'More ...' );
}
I'm using wordpress and my page has the URL http://proservicescontractors.com/services/
But when I go to the page in my dashboard with the above URL, any change I make does not show on the front end. I tried simply duplicating my content and that change did not show on the front end.
Not sure what to do, this has me completely baffled.
Any ideas?
Since they're custom post types, by default, they're not actually loaded into a page per se. You should read up on WordPress's template hierarchy. To give you a rough idea of what's happening:
WP looks at your URL, and since it recognises it as a custom post type archive, it will look for a template to use...
It will first look for archive-$post_type.php, or in your case, archive-services.php
If it can't find that, it will look for archive.php
If it can't find that, it will use index.php
The important thing to note is that archive pages don't actually show up in the admin area, since they simply gather up and display custom posts, so there's nothing for you to edit.
Now, if you really want to edit some content on the Services archive, you have two options:
Edit archive-services.php in a text editor.
This is the quick and dirty option; the downside is that it defies the point of a CMS.
Create a page template with it's own loop
Create a new page template called page-services.php and insert a loop in there to display your custom posts. To get you started:
<?php get_header(); ?>
<?php // The main loop
if (have_posts()) {
while (have_posts()) {
the_post();
}
} else {
echo 'No posts';
}
?>
<?php // Now for the services loop
// WP_Query arguments
// For additional options, see: https://codex.wordpress.org/Class_Reference/WP_Query#Parameters
$args = array (
'post_type' => array( 'services' ),
);
// The Query itself
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Do something with the post
// In your case look at archive-services.php and see what
// that template does inside the loop
}
} else {
// no posts found
}
// Restore original Post Data
// Don't forget this, it's important
wp_reset_postdata();
?>
<?php get_footer();?>
You should then be able to apply that page template to your Services page; it should then display your posts below the page content. One thing to look out for is that WordPress will continue to load archive-services.php whenever you go to http://proservicescontractors.com/services/. While there are ways around this, the easiest fix would be to simply give your new page a different url, such as http://proservicescontractors.com/all-services/
Thanks for your help. I'm using yoast and I wanted to change the title and description. When you pointed out that it was a custom post type archive and not a page, I went back through yoast and found where I could change them under "Titles and Metas" > "Custom Post Type Archives" > "Services"
I'm new to WordPress and I'm thinking about developing some premium themes. I see that a real trend these days is these themes with multiple sections separated in horizontal blocks in the first page. Stuff like this:
<section class="about-us row">
<h1> About us</h1>
<p>Some lorem here </p>
</section>
<section class="features row">
<h1> Features</h1>
<div class="col-1-3">
<h2>Responsive and shit</h2>
<p>Some lorem here </p>
</div>
<div class="col-1-3">
<h2>Free support</h2>
......
</section>
<section class="testimonials">
........
</section>
I'd like to know what's the best or most common approach devs are taking to provide this feature for their end-users.
I see that some of the best selling themes are using page builders managing it as shortcodes, but I'm not planning to use any page builder, at least not in my first theme, I noted that it's quite easy to get a messy code when using them, so I want to start simple.
So guys, can you help me? would the answer be using just shortcodes?
Thank you
Step 1:
I would suggest breaking the layout into sections using the get_template_part() function in your front-page template. The benefit of this is you can simply call for whatever part of the layout you need in any page template like so: get_template_part('testimonials');. You can even use them in the header and footer if you need to.
In your case i'm assuming there are 3 template parts: about us, features, and testimonials. You will need to create 3 PHP files that contain all of the code for each of those 3 parts. The PHP files will need to be located in your template's root folder. The PHP file can obviously utilize PHP however you need it to, but the main idea is that your HTML code for that section or "template part" will be placed in it's own PHP file. If you need to pull posts from wordpress, or perform database queries to generate the content for that section, you can do so individually for each template part in it's own self-contained PHP file. For the purposes of this example, let's just assume that you've called the PHP files for your template parts about_us_part.php, features_part.php, and testimonials_part.php.
After you create your 3 PHP files, making sure they are placed in your template root, you simply place the following lines of code wherever you want that particular section or "template part" to appear in your Wordpress page template. Like so:
<?php get_template_part( 'about_us_part' ); // About Us (about_us_part.php) ?>
<?php get_template_part( 'features_part' ); // Features (features_part.php) ?>
<?php get_template_part( 'testimonials_part' ); // Testimonials (testimonials_part.php) ?>
Basically, get_template_part( '{slug}' ); searches for a filename in your template root matching {slug}.php. So you can name it whatever you want, but if there is no matching PHP file found, obviously nothing will show up. There is one other option for get_template_part() that allows you to specify a name for the template section. However it is optional and not required, you can use it like so:
<?php get_template_part( 'about_us_part', 'about-us' ); // About Us (about_us_part.php) ?>
<?php get_template_part( 'features_part', 'features' ); // Features (features_part.php) ?>
<?php get_template_part( 'testimonials_part', 'testimonials' ); // Testimonials (testimonials_part.php) ?>
You can read more about get_template_part() in the Wordpress codex here:
http://codex.wordpress.org/Function_Reference/get_template_part
Step 2:
Now say you wanted to allow the user to display these template parts using shortcodes, you'd need to give them that ability in your functions.php file. For instance, say we wanted to create 3 shortcodes for each of the 3 template parts above. You can do it pretty easily using the Wordpress Shortcode API. You'd add the following code to your functions.php file:
[about_us]
function themeprefix_about_us_shortcode( $attr ) {
ob_start(); // Start output buffer
get_template_part( 'about_us_part' ); //Get about_us_part.php
return ob_get_clean(); //Clear output buffer
}
add_shortcode( 'about_us', 'themeprefix_about_us_shortcode' );
Once that function is in your functions.php file, along with the matching add_shortcode() function users can call out the About Us section by using the shortcode [about_us]. The two parts of the add_shortcode() function are the shortcode name, and the function that generates the content for the shortcode. Like so: add_shortcode( '{shortcode name}', '{shortcode function}' );
You'd need to create 2 more for your other 2 shortcodes:
[features]
function themeprefix_features_shortcode( $attr ) {
ob_start(); // Start output buffer
get_template_part( 'features_part' ); //Get features_part.php
return ob_get_clean(); //Clear output buffer
}
add_shortcode( 'features', 'themeprefix_features_shortcode' );
[testimonials]
function themeprefix_testimonials_shortcode( $attr ) {
ob_start(); // Start output buffer
get_template_part( 'testimonials_part' ); //Get testimonials_part.php
return ob_get_clean(); //Clear output buffer
}
add_shortcode( 'testimonials', 'themeprefix_testimonials_shortcode' );
Note: I placed "themeprefix" on the front of each function. I'd reccomend replacing that with your theme name, or whatever prefix you might be using on the front of your theme's function names. However the function name can be whatever you want it to be, just be sure to update your add_shortcode() to the new function name.
You can read more about add_shortcode() in the Wordpress codex here:
http://codex.wordpress.org/Function_Reference/add_shortcode
Also, I reccomend reading the Shortcode API page in the codex to learn how to add parameters to your shortcodes:
http://codex.wordpress.org/Shortcode_API
I've been running in circles for like ages trying to edit this widget. Had some trouble formating all that code in here so i will just post a link from pastebin - http://pastebin.com/tdYFAgQD.
What i want and can't achieve, is to display all the posts in a category, in this format (a block with the featured image, then the title then the text), one after another. I want to get rid of that list thingy (that displays x number of posts but in the same window) and have all the post displayed like as i said, one after another in separate blocks.
Here's a picture that may make the things clearer:
http://i.imgur.com/pfHbi.jpg
You should read up on http://codex.wordpress.org/The_Loop_in_Action
To get the 4 blocks to show up like that, you'll have to think more about the HTML / CSS that will be generated. Consider simply getting the 4 divs you want to show up by modifying the loop, then go from there.
Beginning at line 105 in your pastebin is the bit producing those "links" so simply remove these lines of code:
<?php if ( $warrior_posts_list_left->post_count > 1) echo '<ul>'; ?>
<?php } else { ?>
<li>
<span class="icon-file"></span> <?php echowarrior_post_title('35'); ?>
</li>
<?php } ?>
<?php $i = $i + 1; endwhile; ?>
<?php if ( $warrior_posts_list_left->post_count > 1) echo '</ul>'; ?>
Further, you may want to ask your question here https://wordpress.stackexchange.com/ or on the support forum for the particular wordpress plugin you're modifying http://wordpress.org/extend/plugins/
I have a view that pulls in the titles from 3 different content types. One of these content types has a title that should link to an external website, the other 2 types have titles that link to nodes within the Drupal site. Is there a way I can set the Title field to handle links differently depending on what content type the title is from?
Answered thanks to Vlad below!! :)
This is the working code we are using in the views-view-fields--news--block.tpl.php template..
<?php if ($fields['type']->content == 'Event'): ?>
<?php print $fields['title']->content; ?>
<?php endif; ?>
<?php if ($fields['type']->content == 'PATF News'): ?>
<?php print $fields['title']->content; ?>
<?php endif; ?>
<?php if ($fields['type']->content == 'News Link'): ?>
//This link goes to _blank
<?php print $fields['title']->content; ?>
<?php endif; ?>
Drupal 6
In your view settings, add Node: Type to Fields
In Basic settings group click Theme: Information and click Row style output
Copy all content from Row style output into your theme file (should be named something like views-view-fields--viewsname.tpl.php or views-view-fields--viewsname--viewsnamw.tpl.php) in your theme folder.
Modify output where you should check content type and make different output.
Drupal 7
It's pretty similar with difference that you can find Theme: Information in group Advanced and you have to add Content: Type in your Fields group.
In your views-view-fields--xxx--xxx.tpl.php file write something like:
if ($fields['type']->content == 'Page') {
// print title linking to node
print $fields['title']->content;
}
if ($fields['type']->content == 'News') {
// print title linking to other website
print 'http://example.com/'. $fields['title']->content;
}
Improved code
$link = $fields['path']->content;
$title = $fields['title']->content;
$options = array();
if ($fields['type']->content == 'News Link') {
$link = $fields['field_link']->content;
$options['attributes']['target'] = '_blank';
}
print l($title, $link, $options);
I've done this before with the following steps:
Include fields for Content Title, Content Link, and your external
Link.
Hide Content Title and Content Link from view.
Rewrite results for Content Link should be set to the token for
Content Title (both still hidden).
No results behavior for you external link field should be set to the
token for Content Link.
This displays the external link whenever it's present, and will fall back to a the title linked to the original piece of content whenever it's not.